define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cash/index' + location.search,
                    // add_url: 'cash/add',
                    // edit_url: 'cash/edit',
                    del_url: 'cash/del',
                    multi_url: 'cash/multi',
                    // import_url: 'cash/import',
                    table: 'cash',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id',visible: false,operate:false, title: __('User_id')},
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'true', title: __('True'), operate:'BETWEEN'},
                        {field: 'fee', title: __('Fee'), operate:'BETWEEN'},
                        {field: 'way', title: __('Way'), searchList: {"1":__('Way 1'),"2":__('Way 2')}, formatter: Table.api.formatter.normal},
                        {field: 'url', title: __('Url'), operate: false, formatter: Table.api.formatter.url},
                        {field: 'account', title: __('Account'), operate: 'LIKE'},
                        {field: 'truename', title: __('Truename'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'check_time', title: __('Check_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[{
                                name: 'ok',
                                title: __('通过'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa fa-check',
                                url: 'cash/check/status/1',
                                confirm: '通过申请？',
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    if(ret.code==1){
                                        table.bootstrapTable('refresh', {});
                                    }
                                    //如果需要阻止成功提示，则必须使用return false;
                                    return false;
                                },
                                error: function (data, ret) {
                                    console.log(data, ret);
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            },{
                                name: 'no',
                                title: __('不通过'),
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                icon: 'fa fa-close',
                                url: 'cash/check/status/2',
                                confirm: '不通过申请？',
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    if(ret.code==1){
                                        table.bootstrapTable('refresh', {});
                                    }
                                    //如果需要阻止成功提示，则必须使用return false;
                                    return false;
                                },
                                error: function (data, ret) {
                                    console.log(data, ret);
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }], formatter: function (value, row, index) { //隐藏自定义的按钮
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status>0){
                                    $(table).data("operate-ok", null);  //隐藏按钮操作
                                    $(table).data("operate-no", null);  //隐藏按钮操作
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});